package com.how2java.tmall.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.how2java.tmall.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {
	/**
	 *
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param o
	 * @returna. 不需要登录也可以访问的
	 * 如：注册，登录，产品，首页，分类，查询等等
	 * b. 需要登录才能够访问的
	 * 如：购买行为，加入购物车行为，查看购物车，查看我的订单等等
	 * 不需要登录也可以访问的已经确定了，但是需要登录才能够访问，截止目前为止还不能确定，所以这个拦截器就判断如果不是注册，登录，产品这些，就进行登录校验
	 * 1. 准备字符串数组 requireAuthPages，存放那些需要登录才能访问的路径
	 * 2. 获取uri
	 * 3. 去掉前缀/tmall_springboot
	 * 4. 判断是否是以 requireAuthPages 里的开头的
	 * 4.1 如果是就判断是否登陆，未登陆就跳转到 login 页面
	 * 4.2 如果不是就放行
	 * @throws Exception
	 */
	@Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    	HttpSession session = httpServletRequest.getSession();
        String contextPath=session.getServletContext().getContextPath();
        String[] requireAuthPages = new String[]{
        		"buy",
        		"alipay",
        		"payed",
        		"cart",
        		"bought",
        		"confirmPay",
        		"orderConfirmed",
        		
        		"forebuyone",
        		"forebuy",
        		"foreaddCart",
        		"forecart",
        		"forechangeOrderItem",
        		"foredeleteOrderItem",
        		"forecreateOrder",
        		"forepayed",
        		"forebought",
        		"foreconfirmPay",
        		"foreorderConfirmed",
        		"foredeleteOrder",
        		"forereview",
        		"foredoreview"
        		
        };
 
        
        String uri = httpServletRequest.getRequestURI();

        uri = StringUtils.remove(uri, contextPath+"/");
        String page = uri;
        
        
        
        if(begingWith(page, requireAuthPages)){
			User user = (User) session.getAttribute("user");
    		if(user==null) {
            	httpServletResponse.sendRedirect("login");
                return false;
            }
        }
        return true;   
    }

    private boolean begingWith(String page, String[] requiredAuthPages) {
    	boolean result = false;
    	for (String requiredAuthPage : requiredAuthPages) {
			if(StringUtils.startsWith(page, requiredAuthPage)) {
				result = true;	
				break;
			}
		}
    	return result;
	}

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
